HDU 4286 Data Handler 第37届ACM/ICPC 天津赛区网络赛1009题 (双向链表模拟)

Data Handler

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 689    Accepted Submission(s): 159


Problem Description
  You are in charge of data in a company, so you are called "Data Handler". Different from the data in computer, the data you have are really in huge volume, and each data contains only one integer. All the data are placed in a line from left to right. There are two "hand" to handle the data, call hand "L" and hand "R". Every hand is between two adjacent data or at the end of the data line.
  In one day, the company gives you many commands to handle these data, so you should finish them one by one. At the beginning, there are N data, and hand "L" and "R" are in some positions. Each command is one the following formats:
  (1)MoveLeft L/R: it means that you should move the hand "L"/"R" left one data unit;


  (2)MoveRight L/R: it means that you should move the hand "L"/"R" right one data unit;



  (3)Insert L X: it means that you should insert the data that contains X at the right of the hand "L";



  (4)Insert R X: it means that you should insert the data that contains X at the left of the hand "R";



  (5)Delete L: it means that you should delete the one data at the right of the hand "L";



  (6)Delete R: it means that you should delete the one data at the left of the hand "R";



  (7)Reverse: it means that you should reverse all the data between hand "L" and hand "R".


  After finish all the commands, you should record all the data from left to right. So please do it.
 

 

Input
  The first line contains an integer T(1<=T<=10), the number of test cases.
  Then T test cases follow. For each test case, the first line contains an integer N(1<=N<=500000), the number of data at the beginning. The second line contains N integers, means the integer in each data, from left to right. The third line contains two integers L and R (1<=L<=R<=N), the positions of hand "L" and hand "R". It means that hand "L" is at the left of the L-th data and hand "R" is at the right of the R-th data. The fourth line contains one integer M(1<=M<=500000), the number of commands. Then M lines follow, each line contains a command in the above format. All the integers in the data will in range [-10000,10000].
  It is guaranteed that there are always some data between hand "L" and "R", and if the hand is at the left/right end of the data line, it will not receive the command MoveLeft/MoveRight.
  Because of large input, please use scanf instead of cin.
 

 

Output
  For each test case, output the integers in the data from left to right in one line, separated in a single space.
  Because of large output, please use printf instead of cout.
  
 

 

Sample Input
2 5 1 2 3 4 5 1 5 5 MoveLeft R Insert R 6 Reverse Delete R Insert L 7 5 6536 5207 2609 6604 -4046 1 3 5 Delete L Insert R -9221 Reverse Delete L MoveRight L
 

 

Sample Output
7 6 4 3 2 5 2609 5207 6604 -4046
 

 

Source
 

 

Recommend
liuyiding
 
 
 
这么水的题目,比赛时竟然没有做出来。
今天写了下双向链表模拟,感觉很简单,就是麻烦一些。
/*
HDU
G++  4015ms  11968K
双向链表模拟
注意细节
*/

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<algorithm>
using namespace std;
const int MAXN=2000000;
struct Node
{
    int from;
    int to;
    int val;
}node[MAXN];
int tol;

int L,R;//两个指针
int LL,RR;//分别是指针L,R的左侧和右侧,由于倒一下可能改变方向,所以记录下方向
int n;

void moveleft_L(void)//左移L指针
{
    if(L==0)return;//最左侧了
    if(node[L].to==LL)
    {
        LL=L;
        L=node[L].from;
    }
    else
    {
        LL=L;
        L=node[L].to;
    }
}

void moveleft_R()
{
    if(RR==0)return;
    if(node[RR].to==R)
    {
        R=RR;
        RR=node[RR].from;
    }
    else
    {
        R=RR;
        RR=node[RR].to;
    }
}
void moveright_L()
{
    if(LL==n+1)return;
    if(L==node[LL].from)
    {
        L=LL;
        LL=node[LL].to;
    }
    else
    {
        L=LL;
        LL=node[LL].from;
    }
}

void moveright_R()
{
    if(R==n+1)return;
    if(node[R].from==RR)
    {
        RR=R;
        R=node[R].to;
    }
    else
    {
        RR=R;
        R=node[R].from;
    }
}

void insert_L(int v)
{
    node[tol].val=v;
    node[tol].from=L;
    node[tol].to=LL;
    if(node[L].to==LL)node[L].to=tol;
    else node[L].from=tol;
    if(node[LL].from==L)node[LL].from=tol;
    else node[LL].to=tol;
    LL=tol;
    tol++;
}
void insert_R(int v)
{
    node[tol].val=v;
    node[tol].from=RR;
    node[tol].to=R;
    if(node[RR].to==R)node[RR].to=tol;
    else node[RR].from=tol;
    if(node[R].from==RR)node[R].from=tol;
    else node[R].to=tol;
    RR=tol;
    tol++;
}

void del_L()
{
    if(LL==n+1)return;
    if(L==n+1)return;
    int t;
    if(node[LL].from==L)t=node[LL].to;
    else t=node[LL].from;

    if(node[t].from==LL)node[t].from=L;
    else node[t].to=L;

    if(node[L].to==LL)node[L].to=t;
    else node[L].from=t;

    LL=t;
}

void del_R()
{
    if(RR==0)return;
    if(R==0)return;
    int t;
    if(node[RR].to==R)t=node[RR].from;
    else t=node[RR].to;

    if(node[t].from==RR)node[t].from=R;
    else node[t].to=R;

    if(node[R].from==RR)node[R].from=t;
    else node[R].to=t;

    RR=t;
}

void reverse()
{
    if(node[L].to==LL)node[L].to=RR;
    else node[L].from=RR;

    if(node[LL].from==L)node[LL].from=R;
    else node[LL].to=R;

    if(node[R].from==RR)node[R].from=LL;
    else node[R].to=LL;

    if(node[RR].to==R)node[RR].to=L;
    else node[RR].from=L;

    int temp;
    temp=LL;
    LL=RR;
    RR=temp;
}

void output()
{
    int flag=true;
    int tt=0;
    for(int i=node[0].to;i!=n+1;)
    {
        if(flag)flag=false;
        else printf(" ");

        printf("%d",node[i].val);

        if(node[i].from==tt)
        {
            tt=i;
            i=node[i].to;
        }
        else
        {
            tt=i;
            i=node[i].from;
        }
    }
    printf("\n");
}

char str[20];
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int T;
    int m;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&node[i].val);
            node[i].from=i-1;
            node[i].to=i+1;
        }
        node[0].to=1;
        node[0].from=-1;
        node[n+1].from=n;
        node[n+1].to=-1;
        tol=n+2;//结点数
        scanf("%d%d",&LL,&RR);
        L=LL-1;
        R=RR+1;
        scanf("%d",&m);
        while(m--)
        {
            scanf("%s",&str);
            if(strcmp(str,"MoveLeft")==0)
            {
                scanf("%s",&str);
                if(str[0]=='L')moveleft_L();
                else moveleft_R();
            }
            else if(strcmp(str,"MoveRight")==0)
            {
                scanf("%s",&str);
                if(str[0]=='L')moveright_L();
                else moveright_R();
            }
            else if(strcmp(str,"Delete")==0)
            {
                scanf("%s",&str);
                if(str[0]=='L')del_L();
                else del_R();
            }
            else if(strcmp(str,"Insert")==0)
            {
                scanf("%s",&str);
                if(str[0]=='L')
                {
                    int v;
                    scanf("%d",&v);
                    insert_L(v);
                }
                else
                {
                    int v;
                    scanf("%d",&v);
                    insert_R(v);
                }
            }
            else reverse();
        }
        output();
    }
}

 

posted on 2012-09-11 19:07  kuangbin  阅读(590)  评论(1编辑  收藏  举报

导航

JAVASCRIPT: